package org.dizena.third;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import org.dizena.utils.FileHttpType;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.List;

public class AliOssAbcPub
{

    private static final String endpoint = "";
    private static final String accessKeyId = "";
    private static final String accessKeySecret = "";
    private static final OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    private final static String bucketName = "";

    public static boolean putOss(String ossKey, byte[] bs)
    {
        try
        {
            InputStream is = new ByteArrayInputStream(bs);
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(FileHttpType.getContentType(ossKey));
            ossClient.putObject(bucketName, ossKey, is, meta);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean putOss(String ossKey, String localFilePath)
    {
        try
        {
            // 上传文件流
            InputStream is = new FileInputStream(localFilePath);
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(FileHttpType.getContentType(ossKey));
            ossClient.putObject(bucketName, ossKey, is, meta);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean putOss(String ossKey, InputStream is)
    {
        try
        {
            // 上传文件流
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(FileHttpType.getContentType(ossKey));
            ossClient.putObject(bucketName, ossKey, is, meta);
            if (is != null)
            {
                is.close();
            }
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static InputStream getOssInputStream(String ossKey)
    {
        InputStream result = null;
        try
        {
            OSSObject ossObject = ossClient.getObject(bucketName, ossKey);
            result = ossObject.getObjectContent();
        } catch (Exception e)
        {
            return null;
        }
        return result;
    }

    public static byte[] getOssByte(String ossKey)
    {
        try
        {
            OSSObject ossObject = ossClient.getObject(bucketName, ossKey);
            InputStream result = ossObject.getObjectContent();
            byte[] bs = new byte[1024];
            int i = -1;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while ((i = result.read(bs)) > 0)
            {
                bos.write(bs, 0, i);
            }
            result.close();
            byte[] all = bos.toByteArray();
            bos.flush();
            bos.close();
            return all;
        } catch (Exception e)
        {
            return null;
        }
    }

    public static boolean putOssString(String ossKey, String content)
    {
        try
        {
            InputStream is = new ByteArrayInputStream(content.getBytes());
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(FileHttpType.getContentType(ossKey));
            ossClient.putObject(bucketName, ossKey, is, meta);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static String getOssString(String ossKey)
    {
        String result = null;
        try
        {
            OSSObject ossObject = ossClient.getObject(bucketName, ossKey);
            InputStream inputStream = ossObject.getObjectContent();
            StringBuilder objectContent = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            while (true)
            {
                String line = reader.readLine();
                if (line == null) break;
                objectContent.append(line);
            }
            inputStream.close();
            result = objectContent.toString();
        } catch (Exception e)
        {
        }
        return result;
    }

    public static String getOssFileAccessUrl(String ossKey)
    {
        try
        {
            Date expiration = new Date(System.currentTimeMillis() + 6000);
            URL url = ossClient.generatePresignedUrl(bucketName, ossKey, expiration);
            return url.toString();
        } catch (Exception e)
        {
            return null;
        }
    }

    /**
     * 删除 OSS 中的文件
     */
    public static boolean delOssUrlFile(String domain, String url)
    {
        try
        {
            int start = url.indexOf(domain + "/");
            String ossKey = url.substring(start + (domain + "/").length(), url.length());
            ossClient.deleteObject(bucketName, ossKey);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * 删除 OSS 中的文件
     */
    public static boolean delOssFile(String ossKey)
    {
        try
        {
            ossClient.deleteObject(bucketName, ossKey);
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * 删除 OSS 中的文件夹
     */
    public static boolean delOssDir(String ossDir)
    {
        try
        {
            ListObjectsV2Result result = ossClient.listObjectsV2(bucketName, ossDir);
            List<OSSObjectSummary> ossObjectSummaries = result.getObjectSummaries();
            for (OSSObjectSummary s : ossObjectSummaries)
            {
                ossClient.deleteObject(bucketName, s.getKey());
            }
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * 删除 OSS 中的文件
     */
    public static boolean delOssFile(List<String> ossKeyList)
    {
        try
        {
            ossClient.deleteObject(new DeleteObjectsRequest(bucketName).withKeys(ossKeyList));
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * OSS 中的文件是否存在
     */
    public static boolean isExistOssfile(String ossKey)
    {
        try
        {
            return ossClient.doesObjectExist(bucketName, ossKey);
        } catch (Exception e)
        {
            return false;
        }
    }

    /**
     * OSS 中的文件是否存在
     */
    public static boolean isExistOssUrlFile(String domain, String url)
    {
        try
        {
            int start = url.indexOf(domain + "/");
            String ossKey = url.substring(start + (domain + "/").length(), url.length());
            return ossClient.doesObjectExist(bucketName, ossKey);
        } catch (Exception e)
        {
            return false;
        }
    }
}
